package com.sinog.core.util;

import com.sinog.core.utilbean.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Encoder;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @desc 字符串工具类
 * @author lzq
 * @date 2020-08-31 4:14 下午
 */
@Slf4j
public final class StrUtil {

    private static final Integer TWO = 2;

    /**
     * 路径符替换
     * @param realPath 路径符
     * @return String[]
     */
    public static String[] getReplaceEnd(String realPath) {
        String operaOs = System.getProperty("os.name");
        return operaOs.toLowerCase().startsWith("win") ? realPath.replace("/","\\").split("\\\\") : realPath.split("/");
    }

    /**
     * 工具方法 校验目标字符序列是否包含searchSeqs中的元素
     * @param seq 校验目标字符序列
     * @param searchSeqs CharSequence数组
     * @return boolean
     */
    public static boolean contains(CharSequence seq,CharSequence... searchSeqs) {
        for(CharSequence cs : searchSeqs) {
            if(null != seq && null != cs) {
                boolean b = 0 <= indexOf(seq,cs);
                if(b) {
                    return true;
                }
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * 描述
     * @param cs cs
     * @param searchChar searchChar
     * @return int
     */
    private static int indexOf(CharSequence cs,CharSequence searchChar) {
        return cs.toString().indexOf(searchChar.toString());
    }

    /**
     * 工具方法 校验目标字符序列在searchSeqs中
     * @param seq 校验目标字符序列
     * @param searchSeqs searchSeqs
     * @return boolean
     */
    public static boolean containsEquals(CharSequence seq,CharSequence... searchSeqs) {
        for(CharSequence cs : searchSeqs) {
            if(null != seq && null != cs) {
                boolean b = seq.equals(cs);
                if(b) {
                    return true;
                }
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * 工具方法 校验目标字符序列在searchSeqs中
     * @param seq 校验目标字符序列
     * @param msg 传输的字符串
     * @return boolean
     */
    public static boolean stringContainsEquals(CharSequence seq,String msg) {
        String[] split = msg.split(",");
        return containsEquals(seq,split);
    }

    /**
     * map value object类型转换为string
     * @param map map
     * @return Map<String,Object>
     */
    public static Map<String,Object> mapToString(Map<String,Object> map) {
        Map<String,Object> result = new HashMap<>(16);
        //通过key拿到value
        map.keySet().forEach(s -> {
            String val = map.get(s).toString();
            result.put(s,val);
        });
        return result;
    }

    /**
     * List map value object类型转换为string
     * @param list List
     * @return List<Map < String,Object>>
     */
    public static List<Map<String,Object>> listToString(List<Map<String,Object>> list) {
        List<Map<String,Object>> relist = new ArrayList<>(16);
        for(Map<String,Object> map : list) {
            Map<String,Object> result = mapToString(map);
            relist.add(result);
        }
        return relist;
    }

    /**
     * 描述
     * @param str 原有字符串
     * @param num 字符串最小长度
     * @return String
     */
    public static String fillString(String str,int num) {
        if(null != str) {
            int length = str.length();
            if(length < num) {
                for(int i = 0; i < (num - length) / TWO; i++) {
                    str = new StringBuilder(16).append("  ").append(str).append("  ").toString();
                }
                if(0 < (num - length) % TWO) {
                    str = " " + str + "  ";
                }
            }
        } else {
            StringBuilder strBuilder = new StringBuilder(16);
            for(int i = 0; i < num / TWO; i++) {
                strBuilder.append("    ");
            }
            str = strBuilder.toString();
        }
        return str;
    }

    /**
     * 获得图片的base64码
     * @param src 图片地址
     * @return String
     */
    public static String getImageBase(String src) throws IOException {
        if(null == src || Objects.equals(src,"")) {
            return "";
        }
        File file = new File(src);
        if(!file.exists()) {
            return "";
        }
        InputStream in;
        byte[] data = null;
        in = Files.newInputStream(file.toPath());
        try {
            data = new byte[in.available()];
            int read = in.read(data);
            log.info("读取字节数{}",read);
        } catch(IOException e) {
            log.error("发生异常:",e);
        } finally {
            in.close();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

    /**
     * 将字符串装为集合
     * @param str 字符串 “‘’，‘’”
     * @return 集合
     */
    public static List<String> getList(String str) {
        String replace = str.replace("'","");
        String[] splits = replace.split(",");
        List<String> gzryidLists = new ArrayList<>(16);
        Collections.addAll(gzryidLists,splits);
        return gzryidLists;
    }

    /**
     * 参数假如是-1需要转换成null
     * @param str 要转换的字符串
     * @return String
     */
    public static String converseToNull(String str) {
        return Constants.NumberStr.NUM_STR_FU_ONE.equals(str) ? null : str;
    }

    /**
     * level转换，例如：把参数中01格式转换成1
     * @param paramLevel 入参level
     * @return String
     */
    public static String converseLevel(String paramLevel) {
        if(StringUtils.isNotBlank(paramLevel) && Constants.Number.NUM_TWO <= paramLevel.length() && Constants.CharFinal.CHAR_ZERO == paramLevel.charAt(0)) {
            paramLevel = paramLevel.substring(1);
        }
        return paramLevel;
    }
}